package com.itheima;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
public class PoiDemo {

    /**
     * poi写入到excel中
     * @param args
     */
    public static void main(String[] args) throws IOException {
        //1. 创建工作薄对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2. 创建工作表对象
        HSSFSheet sheet = workbook.createSheet("first");


        //3. 创建行对象
        HSSFRow row = sheet.createRow(0);
        //4. 创建单元格对象
        HSSFCell cell = row.createCell(0);
        //5. 给单元格赋值
        cell.setCellValue("猪八戒");
        //4. 创建单元格对象
        cell = row.createCell(1);
        //5. 给单元格赋值
        cell.setCellValue("孙悟空");

        //3. 创建行对象
        row = sheet.createRow(1);
        //4. 创建单元格对象
        cell = row.createCell(0);
        //5. 给单元格赋值
        cell.setCellValue("沙悟净");

        //4. 创建单元格对象
        cell = row.createCell(1);
        //5. 给单元格赋值
        cell.setCellValue("唐三藏");

        //获取输出流对象
        FileOutputStream outputStream = new FileOutputStream("C:\\Users\\sun\\Desktop\\test.xlsx");
        //写入到excel中
        workbook.write(outputStream);

        outputStream.flush();
        outputStream.close();
        workbook.close();

    }

    /**
     * 读取excel数据（二）
     * @param args
     */
    public static void main2(String[] args) throws IOException {
        //获取excel对象（工作薄）
        XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\sun\\Desktop\\test.xlsx");
        //根据名称获取工作表对象
        XSSFSheet sheet1 = workbook.getSheet("Sheet1");
        //获取工作表中的行对象
        for (Row row : sheet1) {
            for (Cell cell : row) {
//                System.out.println(cell);
//                System.out.println(cell.getCellType());
                //可以把数值类型单元格设置为字符串类型
                if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                }

                System.out.println(cell);
            }

        }
    }

    /**
     * 读取excel的数据(一)
     * @param args
     */
    public static void main1(String[] args) throws IOException {
        //获取excel对象（工作薄）
        XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\sun\\Desktop\\test.xlsx");
        //获取(第一个)工作表对象
        XSSFSheet sheet = workbook.getSheetAt(0);
        //获取 行对象
//        XSSFRow row = sheet.getRow(0);
//        System.out.println(row);
        //获取单元格
//        XSSFCell cell = row.getCell(0);
//        System.out.println(cell);

        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            //该行有多少个单元格
            short lastCellNum = row.getLastCellNum();
            for (int j = 0; j < lastCellNum; j++) {
                XSSFCell cell = row.getCell(j);
                System.out.println(cell);
            }
        }
    }
}
